/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test.annotated.parent;

import org.junit.Test;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.annotations.Attribute;
import org.sourceprojects.lycia.annotations.Base64;
import org.sourceprojects.lycia.annotations.Parent;
import org.sourceprojects.lycia.annotations.TextBody;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.fluent.FluentBuilder;
import org.sourceprojects.lycia.test.TestCaseUtilities;

import static org.sourceprojects.lycia.fluent.FluentBuilder.*;
import static org.junit.Assert.*;

public class ParentBase64CombinationInjectionTestCase {

	private static final String XML_SIMPLE = TestCaseUtilities.XML_HEADER
			+ "<test value=\"SGVsbG8=\">V29ybGQ=<testcase-simple value=\"dGVzdA==\">dGVzdA==</testcase-simple></test>";

	private static final String XML_SIMPLE_LAYERED = TestCaseUtilities.XML_HEADER
			+ "<outertest value=\"V29ybGQ=\"><test value=\"SGVsbG8=\">V29ybGQ=<testcase-simple value=\"dGVzdA==\">dGVzdA==</testcase-simple></test>SGVsbG8=</outertest>";

	private static final String XML_TEXTBODY_DEEP_READ = TestCaseUtilities.XML_HEADER
			+ "<outertest>Hello<test>Layered<testcase-simple>World</testcase-simple></test></outertest>";

	private final Object contextObject = new Object();

	@Test
	public void testAnnotationExtendedInjectionNullUnaware()
			throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(nullAware(false),
				contextObject(contextObject)).parser(
				tag("testcase-simple", method("annotationExtendedInjection",
						this, String.class, String.class, byte[].class,
						byte[].class, String.class, byte[].class))).build();

		parser.parse(XML_SIMPLE);
	}

	@Test
	public void testAnnotationExtendedInjectionNullAware()
			throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(
				contextObject(contextObject)).parser(
				tag("testcase-simple", method("annotationExtendedInjection",
						this, String.class, String.class, byte[].class,
						byte[].class, String.class, byte[].class))).build();

		parser.parse(XML_SIMPLE);
	}

	@Test
	public void testLayeredAnnotationExtendedInjectionNullUnaware()
			throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(nullAware(false),
				contextObject(contextObject)).parser(
				tag("testcase-simple", method(
						"layeredAnnotationExtendedInjection", this,
						String.class, String.class, byte[].class, byte[].class,
						String.class, byte[].class))).build();

		parser.parse(XML_SIMPLE_LAYERED);
	}

	@Test
	public void testLayeredAnnotationExtendedInjectionNullAware()
			throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(
				contextObject(contextObject)).parser(
				tag("testcase-simple", method(
						"layeredAnnotationExtendedInjection", this,
						String.class, String.class, byte[].class, byte[].class,
						String.class, byte[].class))).build();

		parser.parse(XML_SIMPLE_LAYERED);
	}

	@Test
	public void testTextBodyDeepReadNullUnaware() throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(nullAware(false),
				contextObject(contextObject))
				.parser(
						tag("testcase-simple", method("textBodyDeepRead", this,
								String.class, byte[].class, String.class,
								byte[].class))).build();

		parser.parse(XML_TEXTBODY_DEEP_READ);
	}

	@Test
	public void testTextBodyDeepReadNullAware() throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(
				contextObject(contextObject))
				.parser(
						tag("testcase-simple", method("textBodyDeepRead", this,
								String.class, byte[].class, String.class,
								byte[].class))).build();

		parser.parse(XML_TEXTBODY_DEEP_READ);
	}

	public void annotationExtendedInjection(
			@Parent @TextBody @Base64 String textBodyBase64,
			@Parent @Base64 String decoded,
			@Parent @TextBody @Base64 byte[] textBodyDecodedArray,
			@Parent @Base64 byte[] decodedArray,
			@Parent @Attribute("value") @Base64 String attributeBase64AsString,
			@Parent @Attribute("value") @Base64 byte[] attributeBase64) {

		assertNotNull(textBodyBase64);
		assertEquals("World", textBodyBase64);

		assertNotNull(decoded);
		assertEquals("World", decoded);

		assertNotNull(attributeBase64AsString);
		assertEquals("Hello", attributeBase64AsString);

		assertNotNull(attributeBase64);
		assertEquals("Hello", new String(attributeBase64));

		assertNotNull(decodedArray);
		assertNotNull(textBodyDecodedArray);

		assertArrayEquals(decodedArray, textBodyDecodedArray);
	}

	public void layeredAnnotationExtendedInjection(
			@Parent(layers = 2) @TextBody @Base64 String textBodyBase64,
			@Parent(layers = 2) @Base64 String decoded,
			@Parent(layers = 2) @TextBody @Base64 byte[] textBodyDecodedArray,
			@Parent(layers = 2) @Base64 byte[] decodedArray,
			@Parent(layers = 2) @Attribute("value") @Base64 String attributeBase64AsString,
			@Parent(layers = 2) @Attribute("value") @Base64 byte[] attributeBase64) {

		assertNotNull(textBodyBase64);
		assertEquals("Hello", textBodyBase64);

		assertNotNull(decoded);
		assertEquals("Hello", decoded);

		assertNotNull(attributeBase64AsString);
		assertEquals("World", attributeBase64AsString);

		assertNotNull(attributeBase64);
		assertEquals("World", new String(attributeBase64));

		assertNotNull(decodedArray);
		assertNotNull(textBodyDecodedArray);

		assertArrayEquals(decodedArray, textBodyDecodedArray);
	}

	public void textBodyDeepRead(
			@Parent @TextBody(deep = true) String textBodyDeep,
			@Parent @TextBody(deep = true) byte[] textBodyDeepArray,
			@Parent(layers = 2) @TextBody(deep = true) String textBodyDeeper,
			@Parent(layers = 2) @TextBody(deep = true) byte[] textBodyDeeperArray) {

		assertNotNull(textBodyDeep);
		assertEquals("Layered\nWorld", textBodyDeep);

		assertNotNull(textBodyDeepArray);
		assertEquals("Layered\nWorld", new String(textBodyDeepArray));

		assertNotNull(textBodyDeeper);
		assertEquals("Hello\nLayered\nWorld", textBodyDeeper);

		assertNotNull(textBodyDeeperArray);
		assertEquals("Hello\nLayered\nWorld", new String(textBodyDeeperArray));
	}

}
